#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMap>
#include <QKeyEvent>
#include "canthread.h"
#include "UdpSocket.h"
#include "protocol256.h"
#include "lc_a6_motor.h"
#include "yeyagangcontrol.h"
#include <QTimer>
#include "dataDefine.h"
#include <QFuture>
#include <QFutureWatcher>
#include <QThreadPool>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#define ROLL_MOTOR_A_ID  4
#define ROLL_MOTOR_B_ID  5

#define Y_MOTOR_ID    3
#define X_MOTOR_ID    2
#define YAW_MOTOR_ID  6


#define Z_YYG  7
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
Q_DECLARE_METATYPE(struct input_event)

enum{
    Y_KEY_CODE =308,
    A_KEY_CODE = 304,
    X_KEY_CODE = 307,
    B_KEY_CODE = 305,
    LEFT_CODE = 310,
    RIGHT_CODE = 311,
    LETF_DOWN_CODE = 317,
    RIGHT_DOWN_CODE = 318,
    LETF_YOUMEN_CODE =10,
    RIGHT_YOUMEN_CODE = 9
};



class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    void ui_init();
    ~MainWindow();
    CanThread *canT;
    CanThread *canT_1;
    UdpSocket *canSocket;
    protocol_parser_t pars_t;
    QWidget *tempWid;
    void MCU_Trans(float x, float y, float z);
    void MCU_Rot(float yaw, float pitch);

    void dataReceived(const QHostAddress &address, quint16 port, const QByteArray &data);
    void setRegResult(float x, float y, float z, float yaw, float pitch,float roll,offset_t offet);
    void sendTest();

    enum{
        MODE_MANUAL,
        MODE_AUTO
    };
    enum{
        STEP_SET_AXI,
        STEP_CONTROL_AXI_X,
        STEP_CONTROL_AXI_Y,
        STEP_CONTROL_AXI_Z,
        STEP_CONTROL_AXI_PITCH,
        STEP_CONTROL_AXI_ROLL,
        STEP_CONTROL_AXI_YAW,
        STEP_CONTROL_AXI
    };

    /********控制******/
    QTimer askMotorPosTimer;
    QTimer autoControlTimer;
    int32_t ConTrolMode;
    int32_t f_recive_cameraResult = 0;
    offset_t camera_result;
    int32_t c_controlStrep = STEP_SET_AXI;
    void askMotorPosTask();
    void autoControlTask();
    void readKeyTask();
    bool KeyThread = true;
    /******电机控制映射************/
    QMap<QString, LC_A6_Motor * > Motor_name_Map;
    QMap<uint8_t ,LC_A6_Motor*  > Motor_id_Map;


    // 液压刚控制映射
    QMap<uint8_t ,YeYaGangControl *  > YYG_id_Map;


    // **************整体流程控制
    enum{
        CAL_POS,
        WAIT_ACTIVE_PO
    };
    int32_t control_x_step;
    void contralTask_AXI_X();
    void showInfo(QString str);
protected:
    void closeEvent(QCloseEvent *event)override;
    void keyPressEvent(QKeyEvent *event)override;

private slots:
    void on_setBtn_clicked();

    void on_setBtn_2_clicked();
    void on_cb_askPos_clicked(bool checked);

    void on_bn_roll_shun_action_clicked();

    void on_setBtn_4_clicked();

    void on_btnEnableMotor_clicked();

    void on_bn_yaw_action_clicked();

    void on_bn_Ypianyi_action_clicked();

    void on_bn_Xpianyi_action_clicked();

    void on_cb_controlMode_clicked(bool checked);

    void on_btn_moNiSet_clicked();

public slots:
    void dealCanMessage(const struct can_frame frame);
    void reciveKeyCode(struct  input_event ev);
private:
    Ui::MainWindow *ui;
signals:
    void newKeyInput(struct  input_event ev);
};
#endif // MAINWINDOW_H
